﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;
using TireStore.Models;

namespace TireStore.Controllers
{
    public class ProductGroupController : Controller
    {
        //
        // GET: /ProductGroup/
        readonly TireStoreContext _db = new TireStoreContext();

        public ActionResult Index(int? page, int? curpage, int? sizpage)
        {
            var pageSize = (sizpage ?? 5);
            var pageNum = (page ?? 1);

            var x = new PagedListRenderOptions
            {
                MaximumPageNumbersToDisplay = 3,
                LinkToFirstPageFormat = "Đầu",
                LinkToLastPageFormat = "Cuối",
                LinkToPreviousPageFormat = "«",
                LinkToNextPageFormat = "»",
                DisplayLinkToFirstPage = PagedListDisplayMode.Always,
                DisplayLinkToLastPage = PagedListDisplayMode.Always,
                DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
                DisplayLinkToNextPage = PagedListDisplayMode.Always
            };

            ViewBag.CustomPage = x;
            var pg = _db.ProductGroups.ToList().ToPagedList(pageNum, pageSize);

            return View(pg);
        }


        public ActionResult Create()
        {
            ViewBag.ProductGroupId = new SelectList(_db.ProductGroups, "Id", "Name");
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection form, ProductGroupInfo prGroup)
        {
            prGroup.Active = form["ddlVisible"] == "True";

            if (form["ProductGroupId"] != "")
            {
                var e = form["ProductGroupId"];
                prGroup.ParentId = Int32.Parse(form["ProductGroupId"]);
            }


            if (ModelState.IsValid)
            {
                var x = new ProductGroup
                {
                    Name = prGroup.Name,
                    Description = prGroup.Description,
                    MIndex = prGroup.MIndex,
                    Active = prGroup.Active,
                    ParentId = prGroup.ParentId
                };

                _db.ProductGroups.Add(x);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ProductGroupId = new SelectList(_db.ProductGroups, "Id", "Name");
            return View();
        }


        public ActionResult Edit(int id)
        {
            var prGroup = _db.ProductGroups.Find(id);
            if (prGroup != null)
            {
                var x = new ProductGroupInfo
                {
                    Id = prGroup.Id,
                    Name = prGroup.Name,
                    Description = prGroup.Description,
                    MIndex = prGroup.MIndex,
                    Active = prGroup.Active
                };

                ViewBag.ParentId = new SelectList(_db.ProductGroups.Where(m => m.Id != prGroup.Id), "Id", "Name", prGroup.ParentId);

                //List<SelectListItem> States = new List<SelectListItem>() { 
                //    new SelectListItem() { Text="Gốc", Value="0"},
                //    new SelectListItem() { Text="Alabama", Value="AL"},
                //    new SelectListItem() { Text="----Alaska", Value="AK"},
                //    new SelectListItem() { Text="--------Alaska", Value="AK"},
                //};
                //ViewBag.tttt = States;

                return View(x);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(FormCollection form, ProductGroupInfo prGroup)
        {

            prGroup.Active = form["ddlVisible"] == "True";

            if (form["ParentId"] != "")
            {
                var e = form["ParentId"];
                prGroup.ParentId = Int32.Parse(form["ParentId"]);
            }

            if (ModelState.IsValid)
            {
                var pg = _db.ProductGroups.Find(prGroup.Id);
                if (pg != null)
                {

                    pg.Id = prGroup.Id;
                    pg.Name = prGroup.Name;
                    pg.Description = prGroup.Description;
                    pg.MIndex = prGroup.MIndex;
                    pg.Active = prGroup.Active;
                    pg.ParentId = prGroup.ParentId;
                }

                //if (parentid != 0)
                //{
                //    x.ParentId = parentid;
                //}
                _db.Entry(pg).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ParentId = new SelectList(_db.ProductGroups.Where(m => m.Id != prGroup.Id), "Id", "Name", prGroup.ParentId);
            return View();
        }
    }
}
